﻿<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="http://wpfglue.wordpress.com/commanding"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    xmlns:l="http://wpfglue.wordpress.com/localization"
    xmlns:sc="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    Title="CommandingExample" Height="392" Width="525">
    <Window.Resources>
        <c:CommandSet x:Key="commands">
            <c:ActionCommand Command="Open" ExecutedAction="{c:Action Action=Open}" WorkingMessage="Opening..." ReadyMessage="Opened!"/>
            <c:ActionOfObjectCommand Command="Find" ExecutedAction="{c:Action Action=DisplayItem}" CanExecuteFunc="{c:Func Func=CanDisplayItem}" WorkingMessage="Finding..." ReadyMessage="Found!"/>
            <c:ActionCommand Command="Print" ExecutedAction="{c:Action Action=InvalidOperation}"/>
            <c:ActionCommand Command="Stop" ExecutedAction="{c:Action Action=LongRunningOperation}" WorkingMessage="Running Long..." ReadyMessage="Finally Finished!"/>
            <c:FillCollectionCommand Command="Refresh" DoWorkHandler="{c:Action Action=DoFillItems}" WorkingMessage="Filling Collection..." ReadyMessage="Collection Filled!" ClearBeforeFill="True"/>
            <c:VisitCollectionCommand Command="BrowseBack" ItemAction="{c:Action Action=Reverse}"
 WorkingMessage="Reversing Items..." ReadyMessage="Items Reversed!"/>
        </c:CommandSet>
        <l:ErrorMessageConverter x:Key="errorMessageTranslator">
            <l:ErrorMessage Exception="InvalidOperationException"
                            MessageRegEx="This operation is ([^!]*)!" Format="Don't try {1} operations!"/>
        </l:ErrorMessageConverter>
        <CollectionViewSource x:Key="testItems" Source="{Binding TestItems}" Filter="CollectionViewSource_Filter">
            <CollectionViewSource.SortDescriptions>
                <sc:SortDescription PropertyName="Name"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <DockPanel>
        <StatusBar DockPanel.Dock="Bottom" Height="23">
            <StatusBarItem>
                <StatusBarItem.Content>
                    <MultiBinding>
                        <MultiBinding.Converter>
                            <c:FunnelConverter/>
                        </MultiBinding.Converter>
                        <Binding Source="{StaticResource commands}" Path="Commands[Open].Status"/>
                        <Binding Source="{StaticResource commands}" Path="Commands[Find].Status"/>
                        <Binding Source="{StaticResource commands}" Path="Commands[Print].Status"/>
                        <Binding Source="{StaticResource commands}" Path="Commands[Stop].Status"/>
                        <Binding Source="{StaticResource commands}" Path="Commands[Refresh].Status"/>
                        <Binding Source="{StaticResource commands}" Path="Commands[BrowseBack].Status"/>
                        <Binding Source="{StaticResource commands}" Path="Commands[Print].Exception" Converter="{StaticResource errorMessageTranslator}"/>
                        
                    </MultiBinding>
                </StatusBarItem.Content>
            </StatusBarItem>
        </StatusBar>
        <StackPanel c:Commanding.CommandSet="{StaticResource commands}">
        <Button Command="Open">Open</Button>
        <ListBox x:Name="ItemListBox">
            <ListBox.Items>
                <s:String>Item 1</s:String>
                <s:String>Item 2</s:String>
                <s:String>Item 3</s:String>
            </ListBox.Items>
        </ListBox>
        <Button Command="Find" CommandParameter="{Binding ElementName=ItemListBox, Path=SelectedItem}">Display selected Item</Button>
            <Button Command="Stop">Longrunning Operation</Button>
                <Button Command="Print">Invalid Operation</Button>
            <Label ContentStringFormat="Original Exception Message: {0}" Content="{Binding Source={StaticResource commands}, Path=Commands[Print].Exception.Message}"/>
            <TextBox Text="{Binding Count}"/>
            <Button Command="Refresh" CommandParameter="{Binding TestItems}">Fill List</Button>
            <ListBox ItemsSource="{Binding Source={StaticResource testItems}}" Height="100" DisplayMemberPath="Name" />
            <Button Command="BrowseBack" CommandParameter="{Binding TestItems}">Reverse</Button>
    </StackPanel>
    </DockPanel>
</Window>
